package com.masterclient.app.modules.main.adapter;

import android.graphics.Color;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.eoner.baselibrary.utils.ViewTransformUtil;
import com.masterclient.app.R;
import com.masterclient.app.modules.main.beans.ScanWelfareItemBean;

/**
 * @auther hjian
 * @date 2020/7/27.
 */
public class ScanWelfareAdapter extends BaseQuickAdapter<ScanWelfareItemBean, BaseViewHolder> {

    private int selectIndex = -1;
    private boolean isCanSelect;

    public int getSelectIndex() {
        return selectIndex;
    }

    public ScanWelfareAdapter(boolean isCanSelect) {
        super(R.layout.item_scan_welfare);
        this.isCanSelect = isCanSelect;
    }

    public void setSelectIndex(int selectIndex) {
        if (this.selectIndex == selectIndex) return;
            this.selectIndex = selectIndex;
        notifyDataSetChanged();
    }

    @Override
    protected void convert(BaseViewHolder helper, ScanWelfareItemBean item) {
        ViewTransformUtil.glideImageView(mContext,item.getIcon(),helper.getView(R.id.item_scan_welfare_img),
                new Transformation[]{new CenterCrop()}, R.mipmap.defaults);

        TextView title = helper.getView(R.id.item_scan_welfare_title);
        TextView subTitle = helper.getView(R.id.item_scan_welfare_subtitle);
        ImageView selectBtn = helper.getView(R.id.item_scan_welfare_select);

        title.setText(item.getTitle());
        subTitle.setText(item.getSub_title());
        if (isCanSelect){
//            if (selectIndex == -1 && !item.isIs_sold_out()) selectIndex = helper.getAdapterPosition();
            selectBtn.setVisibility(View.VISIBLE);
            helper.setGone(R.id.item_scan_welfare_received, item.isIs_sold_out());
            title.setTextColor(Color.parseColor(item.isIs_sold_out()?"#4D141922":"#FF141922"));
            subTitle.setTextColor(Color.parseColor(item.isIs_sold_out()?"#4D525866":"#FF999FAA"));
            selectBtn.setImageResource(item.isIs_sold_out()?R.mipmap.single_choice_ban:
                    selectIndex == helper.getAdapterPosition()?R.mipmap.single_choice_select:R.mipmap.single_choice_default);
        }
    }
}
